// pages/seckill_detail/seckill_detail.js
import api from "../../utils/http"
import tool from "../../utils/click_event";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        hour:0,
        expire_time: 0,
        btn_disable: false,
        show: false,
        count: 1,
        spec_attr_value_ids: 0,
        comment:[],
    FavorableRate:'',
    comment_count:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        //判断是否登录
        // var token = wx.getStorageSync('token')
        // if(token==''){
        //   wx.showToast({
        //     title: '请先登录',
        //   })
        //   // wx.navigateTo({
        //   //   url: '/pages/login/login',
        //   // })
        //   return
        // }
        var time = new Date().getHours();
        this.setData({
            hour:time
        })
        let id = options.id;
        this.list(id)
    
     
    },

    list(id) {
        let data = {
            id,
            hour:this.data.hour
        }
        let that = this
        api.getData('/front/secKill/findSecKill', 'get', data).then(res => {
            if (res.code == 200) {
                let now_time = Math.round(new Date().getTime() / 1000).toString();
                let expire_time = res.data.end_time - now_time;
                if (res.data.expire_time < 0) {
                    that.setData({
                        btn_disable: true
                    })
                }
                //获取当前商品的秒杀倒计时
                that.setData({
                    expire_time,
                    list: res.data,
                })
                console.log(res.data.goods_id)
                that.getComment(res.data.goods_id)
            }
        })
    },
    //选择规格
    chooseSpec(e) {
        let spec_attr_value_ids = e.currentTarget.dataset.index
        this.setData({
            spec_attr_value_ids
        })
    },
    //控制遮罩层的显示
    onHidePupopTap() {
        this.setData({
            show: true
        })
    },
    //购买，节流
    buy: tool.throttle(function () {
        //获取用户id
        let uid = 1
        //商品id 数量 ，规格
        let data = {
            uid,
            ids: this.data.list.goods_id,
            order_shop_num: this.data.count,
            spec_attr_value_ids: this.data.spec_attr_value_ids,
            order_total_price: this.data.list.price * this.data.count,
            order_real_price: this.data.list.origin_price,
            pic:this.data.list.pic,
            order_type_id:3//秒杀订单
        }
        let info = {
            uid,
            seckill_id: this.data.list.id,
            goods_id:this.data.list.goods_id,
            count: this.data.count
        }
        //后台抢购
        api.getData('/front/secKill/redisLock', 'post', info).then(res => {
            if (res.code == 200) {
                //抢购成功，跳转订单页面
                wx.showToast({
                    title: '抢购成功',
                    icon: 'success',
                    duration: 5000
                })
                data = JSON.stringify(data)
                //跳转订单页面
                setTimeout(() => {
                    wx.redirectTo({
                        url: '/pages/settlement/settlement?data=' + data,
                    })
                }, 2000);

            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })

    }),
    //获取数量
    count(e) {
        this.setData({
            count: e.detail.count
        })
    },
    //获取评论
    getComment(id){
        // 路由
        let url = '/front/secKill/comment'
        let method = 'GET'
        let data = {
          id:id
        }
        // 异步回调
        api.getData(url,method,data).then((res)=>{
          this.setData({
            comment:res.data.comment,
            comment_count:res.data.count,
            FavorableRate:res.data.FavorableRate
          })
        })
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})